﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoDetailServiceRef;
    using WMS6.Web.UserControls;

    public class BomList : BasePage
    {
        protected Button btnAction;
        protected Button btnDelete;
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnNewSearch;
        protected Button btnReSearch;
        protected HtmlForm F2122;
        protected GridView gvBom;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected Label lblArea;
        protected Label lblMsg;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2BomList(BomQueryEntity bomQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(bomQueryEntity.CurrentPage);
            DataSet ds = new DataSet();
            ds = BaseInfoDetailServiceClientProxy.GetInstance().GetDetaiInfolByQueryEntity(bomQueryEntity, "BOM");
            base.BindData2GridView(this.gvBom, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> selectedID = base.GetSelectedIDsInGridView(this.gvBom, "OWNER_ID");
                List<string> SkuID = base.GetSelectedIDsInGridView(this.gvBom, "SKU_ID");
                List<string> pk3 = new List<string>();
                if ((selectedID == null) || (selectedID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C008");
                }
                else
                {
                    this.lblMsg.Text = BaseInfoDetailServiceClientProxy.GetInstance().DeleteDetail(selectedID, SkuID, pk3, "BOM") + base.GetGlobalMsgResource("C006");
                    BomQueryEntity bomQuery = this.GetBomQueryEntity();
                    this.BindData2BomList(bomQuery);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvBom, "OWNER_ID");
            List<string> SkuIDs = base.GetSelectedIDsInGridView(this.gvBom, "SKU_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("008");
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("009");
            }
            else
            {
                base.Response.Redirect(string.Format("Bom.aspx?from=list&ID={0}&SkuID={1}", base.UrlEncode(selectedID[0]), base.UrlEncode(SkuIDs[0])));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            BomQueryEntity bomQueryEntity = this.GetBomQueryEntity();
            bomQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = bomQueryEntity.CurrentPage;
            bomQueryEntity.CurrentPage = "0";
            this.gvBom.Columns[1].Visible = false;
            this.gvBom.AllowSorting = false;
            this.BindData2BomList(bomQueryEntity);
            this.hdnActionID.Value = "";
            this.gvBom.ShowFooter = false;
            bomQueryEntity.IsGetAll = false;
            bomQueryEntity.CurrentPage = currentPageIndex;
            this.SetBomQueryEntity(bomQueryEntity);
            base.Export2Excel("Bom.xls", this.gvBom);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            BomQueryEntity bomQueryEntity = this.GetBomQueryEntity();
            bomQueryEntity.CurrentPage = "0";
            this.BindData2BomList(bomQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            BomQueryEntity bomQueryEntity = this.GetBomQueryEntity();
            bomQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2BomList(bomQueryEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            BomQueryEntity bomQueryEntity = this.GetBomQueryEntity();
            bomQueryEntity.OwnerID = "";
            bomQueryEntity.SkuID = "";
            bomQueryEntity.Remark = "";
            bomQueryEntity.IsActive = "";
            this.BindData2BomList(bomQueryEntity);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            BomQueryEntity bomQueryEntity = this.GetBomQueryEntity();
            if (int.Parse(bomQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                bomQueryEntity.CurrentPage = (int.Parse(bomQueryEntity.CurrentPage) + 1).ToString();
                this.BindData2BomList(bomQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            BomQueryEntity bomQueryEntity = this.GetBomQueryEntity();
            if (int.Parse(bomQueryEntity.CurrentPage) > 0)
            {
                bomQueryEntity.CurrentPage = (int.Parse(bomQueryEntity.CurrentPage) - 1).ToString();
                this.BindData2BomList(bomQueryEntity);
            }
        }

        private BomQueryEntity GetBomQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as BomQueryEntity);
            }
            return (base.PageQueryEntitySession as BomQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                BomQueryEntity bomQueryEntity = this.GetBomQueryEntity();
                bomQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2BomList(bomQueryEntity);
            }
        }

        protected void gvBom_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F2122", this.gvBom);
        }

        protected void gvBom_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
            }
        }

        protected void gvBom_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                BomQueryEntity bomQuery = this.GetBomQueryEntity();
                bomQuery.SortField = e.SortExpression;
                if (bomQuery.SortDirection == "ASC")
                {
                    bomQuery.SortDirection = "DESC";
                }
                else if (bomQuery.SortDirection == "DESC")
                {
                    bomQuery.SortDirection = "ASC";
                }
                this.BindData2BomList(bomQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            BomQueryEntity bomQueryEntity = new BomQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                bomQueryEntity.OwnerID = this.PreviousPage.OwnerID;
                bomQueryEntity.SkuID = this.PreviousPage.SkuID;
                bomQueryEntity.Remark = this.PreviousPage.BomRemark;
                bomQueryEntity.IsActive = this.PreviousPage.IsActive;
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            bomQueryEntity.IsGetAll = false;
            bomQueryEntity.CurrentPage = "0";
            bomQueryEntity.PageSize = this.gvBom.PageSize.ToString();
            bomQueryEntity.SortField = "ROWID";
            bomQueryEntity.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvBom.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetBomQueryEntity(bomQueryEntity);
            this.BindData2BomList(bomQueryEntity);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblMsg.Text = "";
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetBomQueryEntity(BomQueryEntity bomQueryEntity)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = bomQueryEntity;
            }
            else
            {
                base.PageQueryEntitySession = bomQueryEntity;
            }
        }

        public BomQuery PreviousPage
        {
            get
            {
                return (BomQuery) base.PreviousPage;
            }
        }
    }
}

